/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.ServiceSetting;
import com.ybkj.daijia.serverUtils.ServiceUtil;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.AdminMapper;
import com.ybkj.daijia.server.mapper.AdminRoleMapper;
import com.ybkj.daijia.server.mapper.CompanyMapper;
import com.ybkj.daijia.server.mapper.DriverChoseLevelCarMapper;
import com.ybkj.daijia.server.mapper.DriverChoseLevelFreightMapper;
import com.ybkj.daijia.server.mapper.DriverChoseLevelMapper;
import com.ybkj.daijia.server.mapper.DriverChoseLevelPaotuiMapper;
import com.ybkj.daijia.server.mapper.DriverConsumedRecordMapper;
import com.ybkj.daijia.server.mapper.DriverMapper;
import com.ybkj.daijia.server.mapper.DriverRechargeRecordMapper;
import com.ybkj.daijia.server.mapper.MemberMapper;
import com.ybkj.daijia.server.mapper.OrderMapper;
import com.ybkj.daijia.server.mapper.OrderStatusMapper;
import com.ybkj.daijia.server.mapper.SmsTempletMapper;
import com.ybkj.daijia.server.mapper.UsuallyDriverMapper;
import com.ybkj.daijia.server.mapper.WxBindMapper;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverChoseLevel;
import com.ybkj.daijia.server.model.DriverChoseLevelCar;
import com.ybkj.daijia.server.model.DriverChoseLevelFreight;
import com.ybkj.daijia.server.model.DriverChoseLevelPaotui;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.SuperviceService;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 *
 */
@Service
public class DefaultCompanyService implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Autowired
    private WxBindMapper wxBindMapper;

    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private UsuallyDriverMapper usuallyDriverMapper;

    @Autowired
    private DriverConsumedRecordMapper driverConsumedRecordMapper;

    @Autowired
    private DriverRechargeRecordMapper driverRechargeRecordMapper;

    @Autowired
    private DriverChoseLevelMapper driverChoseLevelMapper;

    @Autowired
    private DriverChoseLevelCarMapper driverChoseLevelCarMapper;

    @Autowired
    private DriverChoseLevelPaotuiMapper driverChoseLevelPaotuiMapper;

    @Autowired
    private DriverChoseLevelFreightMapper driverChoseLevelFreightMapper;

    @Autowired
    private SmsTempletMapper smsTempletMapper;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private SuperviceService superviceService;

    @Override
    public List<Company> getCompanysById(String companyId) {
        List<Company> list = companyMapper.getCompanysById(companyId);
        return list;
    }

    @Override
    public Page<Company> queryAll(String param) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        String likeParam = null;

        if (StringUtils.isNotBlank(param)) {
            likeParam = "%" + param + "%";
        }

        Long companyId = null;
        if (!principal.isCompanyType()) {
            companyId = principal.getCompanyId();
        }

        List<Company> content = companyMapper
            .selectWithParams(likeParam, companyId, Pager.getOffset(), Pager.getLimit());
        Integer total = companyMapper.countWithParams(likeParam, companyId);
        return new PageImpl<Company>(content, pageable, total);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Company create(Company company) {

        companyMapper.insertRecord(company);

        ServiceSetting serviceSetting = ServiceUtil.checkOpen();

        List<Driver> drivers = driverMapper.selectAll();
        if (!CollectionUtils.isEmpty(drivers)) {
            for (Driver driver : drivers) {

                if (serviceSetting.isDaijia()) {
                    DriverChoseLevel driverChoseLevel = new DriverChoseLevel();
                    driverChoseLevel.setDriverId(driver.getId());
                    driverChoseLevel.setCompanyId(company.getId());
                    driverChoseLevel.setDriverlevelId(0L);
                    driverChoseLevelMapper.insertRecord(driverChoseLevel);
                }

                if (serviceSetting.isZhuanche()) {
                    DriverChoseLevelCar driverChoseLevelCar = new DriverChoseLevelCar();
                    driverChoseLevelCar.setDriverId(driver.getId());
                    driverChoseLevelCar.setCompanyId(company.getId());
                    driverChoseLevelCar.setDriverlevelId(0L);
                    driverChoseLevelCarMapper.insertRecord(driverChoseLevelCar);
                }

                if (serviceSetting.isPaotui()) {
                    DriverChoseLevelPaotui driverChoseLevelPaotui = new DriverChoseLevelPaotui();
                    driverChoseLevelPaotui.setDriverId(driver.getId());
                    driverChoseLevelPaotui.setCompanyId(company.getId());
                    driverChoseLevelPaotui.setDriverlevelId(0L);
                    driverChoseLevelPaotuiMapper.insertRecord(driverChoseLevelPaotui);
                }

                if (serviceSetting.isFreight()) {
                    DriverChoseLevelFreight driverChoseLevelFreight = new DriverChoseLevelFreight();
                    driverChoseLevelFreight.setDriverId(driver.getId());
                    driverChoseLevelFreight.setCompanyId(company.getId());
                    driverChoseLevelFreight.setDriverlevelId(0L);
                    driverChoseLevelFreightMapper.insertRecord(driverChoseLevelFreight);
                }

            }
        }

        return company;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Company save(Company company) {

        companyMapper.insertRecord(company);

        return company;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Company company, String basePath) {

        companyMapper.updateByPrimaryKey(company);

        //上传监管平台
        final Company threadCompany = company;
        final String threadBasePath = basePath;

        new Thread(new Runnable() {
            @Override
            public void run() {
                superviceService.JgxxMessage(threadCompany, threadBasePath);
            }
        }).start();
    }

    @Override
    public Company findOne(Long id) {

        return companyMapper.selectByPrimaryKey(id);

    }

    @Override
    public List<Company> findByIdIn(Long[] ids) {

        return companyMapper.selectByIdIn(ids);
    }

    // @Transactional(rollbackFor=Exception.class)
    // public void delete(Company company) {
    //
    // orderStatusMapper.deleteByCompany(company.getId());
    // orderMapper.deleteByCompany(company.getId());
    //
    // //账号相关
    // List<Admin> admins=adminMapper.selectByCompany(company.getId());
    // for (Admin admin : admins) {
    // // 删除管理员与角色的关联
    // adminRoleMapper.deleteByAdminId(admin.getId());
    // // 删除管理员
    // adminMapper.deleteByPrimaryKey(admin.getId());
    // }
    //
    // //活动相关
    // List<Activity> activitys=activityMapper.selectByCompany(company.getId(),
    // null, null);
    // if(CollectionUtils.isNotEmpty(activitys)){
    // for (Activity activity : activitys) {
    // List<ActivityRecord>
    // activityRecords=activityRecordMapper.selectByActivity(activity.getId(),
    // null, null);
    // if(CollectionUtils.isNotEmpty(activityRecords)){
    // for (ActivityRecord activityRecord : activityRecords) {
    // Coupon
    // coupon=couponMapper.selectByActivityRecord(activityRecord.getId());
    // if(null!=coupon){
    // couponMapper.deleteByPrimaryKey(coupon.getId());
    // }
    // activityRecordMapper.deleteByPrimaryKey(activityRecord.getId());
    // }
    // }
    // activityMapper.deleteByPrimaryKey(activity.getId());
    // }
    // }
    //
    // //客户相关
    // List<Member> members=memberMapper.selectByCompany(company.getId());
    // if(CollectionUtils.isNotEmpty(members)){
    // for (Member member : members) {
    // //删除微信绑定信息
    // wxBindMapper.deleteByMember(member.getId());
    // //删除会员账号信息
    // memberAccountMapper.deleteByMember(member.getId());
    // //删除会员财务信息
    // memberPaymentsRecordMapper.deleteByMember(member.getId());
    // //删除会员财务信息
    // memberRechargeRecordMapper.deleteByMember(member.getId());
    // //删除会员财务信息
    // memberConsumedRecordMapper.deleteByMember(member.getId());
    //
    // couponMapper.deleteByMember(member.getId());
    //
    // activityRecordMapper.deleteByMember(member.getId());
    //
    // //删除会员信息
    // memberMapper.deleteByPrimaryKey(member.getId());
    // }
    // }
    //
    // //司机相关
    // List<Driver> drivers=driverMapper.selectAllByCompany(company.getId());
    // if(CollectionUtils.isNotEmpty(drivers)){
    // for (Driver driver : drivers) {
    // usuallyDriverMapper.deleteByDriverId(driver.getId());
    // driverRechargeRecordMapper.deleteByDriverId(driver.getId());
    // driverConsumedRecordMapper.deleteByDriverId(driver.getId());
    // driverMapper.deleteByPrimaryKey(driver.getId());
    // }
    // }
    //
    // //短信相关
    // smsTempletMapper.deleteByCompany(company.getId());
    //
    // companyMapper.deleteByPrimaryKey(company.getId());
    //
    // }

    @Override
    public List<Company> findAll() {
        return companyMapper.selectAll();
    }

    @Override
    public Long countAll() {
        return companyMapper.countAll();
    }

    @Override
    public Company getMain() {

        return companyMapper.selectMainCompany();
    }

    @Override
    public Company findByCompanyName(String companyName) {
        return companyMapper.selectByCompanyName(companyName);
    }

    @Override
    public Company findByCompanyWebURL(String companyWebURL) {
        return companyMapper.selectByCompanyWebURL(companyWebURL);
    }

    @Override
    public Company findByAbbreviation(String abbreviation) {
        return companyMapper.selectByAbbreviation(abbreviation);
    }

    @Override
    public Company findByAppKey(String appKey) {
        return companyMapper.selectByAppKey(appKey);
    }

    @Override
    public List<Company> findByArea(Long areaId) {

        List<Long> array = new LinkedList<Long>();
        array.add(areaId);
        return companyMapper.selectByAreaIdIn(array);

    }

    @Override
    public List<Company> findByAreaCityName(String cityName) {

        return companyMapper.selectByAreaCityName(cityName);
    }

    @Override
    public List<Company> queryAllData(String param) {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        // Pageable pageable = new PageRequest(Pager.getPage(),
        // Pager.getLimit());

        String likeParam = null;

        if (StringUtils.isNotBlank(param)) {
            likeParam = "%" + param + "%";
        }

        Long companyId = null;
        if (!principal.isCompanyType()) {
            companyId = principal.getCompanyId();
        }

        List<Company> list = companyMapper.selectAllByParams(likeParam, companyId);
        return list;
    }

}